<?php


namespace app\admin\controller\product;


use app\admin\model\product\ProductModel;
use core\basic\Controller;

class ProductController extends Controller
{
    private $model;

    public function __construct()
    {
        $this->model = new ProductModel();
    }

    public function index()
    {

        if ((!!$id = get('id', 'int')) && $result = $this->model->getProduct($id)) {
            $this->assign('more', true);
            $this->assign('fields', $this->model->getFields());
            $this->assign('member', $result);
        } else {
            $this->assign('list', true);
            if (!!($field = get('field', 'var')) && !!($keyword = get('keyword', 'vars'))) {
                $result = $this->model->findProduct($field, $keyword);
            } else {
                $result = object_array($this->model->getList());
            }
            // 会员等级

            $this->assign('product', array_to_object($result));
        }
        $this->display('product/Product.html');
    }

    public function add()
    {
        if ($_POST) {
            $product_name = post('product_name');
            $price = post('price');
            $product_pic = post('product_pic');
            $days = post('days');
            $ratio = post('ratio');
            $status = post('status') ?: 1;
            $amount = post('amount') ?: 1;
            $describe = post('describe');
            $data = array(
                'product_name' => $product_name,
                'price' => $price,
                'product_pic' => $product_pic,
                'days' => $days,
                'ratio' => $ratio,
                'status' => $status,
                'amount' => $amount,
                'describe' => $describe,
                'add_time' => get_datetime(),
            );
            // 执行添加
            if ($this->model->addProduct($data)) {
                $this->log('新增产品成功！');
                if (!!$backurl = get('backurl')) {
                    success('新增成功！', base64_decode($backurl));
                } else {
                    success('新增成功！', url('/admin/Product/index'));
                }
            } else {
                $this->log('新增产品失败！');
                error('新增失败！', -1);
            }
        }
    }

    public function mod()
    {


        if (!$id = get('id', 'int')) {
            error('传递的参数值错误！', -1);
        }
        // 单独修改状态
        if (($field = get('field', 'var')) && !is_null($value = get('value', 'var'))) {
            if ($this->model->modProduct($id, "$field='$value'")) {
                location(-1);
            } else {
                alert_back('修改失败！');
            }
        }
        if ($_POST) {
            $data = array(
                'product_name' => post('product_name'),
                'price' => post('price'),
                'product_pic' =>  post('product_pic'),
                'days' => post('days'),
                'ratio' => post('ratio'),
                'status' => post('status') ?: 1,
                'amount' =>  post('amount') ?: 1,
                'describe' =>  post('describe'),
                'add_time' => get_datetime(),
            );
            // 执行修改
            if ($this->model->modProduct($id, $data)) {
                $this->log('修改产品成功！');
                if (!!$backurl = get('backurl')) {
                    success('修改成功！', base64_decode($backurl));
                } else {
                    success('修改成功！', url('/admin/Product/index'));
                }
            } else {
                $this->log('修改会员失败！');
                error('修改失败！', -1);
            }

        } else {
            if (!$result = $this->model->getProduct($id)) {
                error('编辑的内容已经不存在！', -1);
            }
            $this->assign('product', $result);
            $this->assign('mod', true);
            $this->display('product/product.html');
        }

    }
    public  function  del(){
        if (!$id = get('id', 'int')) {
            error('传递的参数值错误！', -1);
        }
        if ($this->model->delProduct($id)) {
            $this->log('删除产品' . $id . '成功！');
            success('删除成功！', -1);
        } else {
            $this->log('删除产品' . $id . '失败！');
            error('删除失败！', -1);
        }
    }
}